<?php

namespace App\Http\Controllers\Landscaping;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Landscaping\GreenDevice;
use App\Models\Landscaping\GreenDeviceFile;
use App\Models\SystemManagement\Employee;
use Illuminate\Database\QueryException;
use Grimzy\LaravelMysqlSpatial\Types\Point;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

/**
 * @group 基础信息管理->园林数据导入->基础设施
 *
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */
class GreenDeviceController extends Controller
{
    /**
     * 基础设施查看列表
     *
     * @bodyParam name string 设施名称
     * @bodyParam location point 经纬度-点 115.24556 25.14551
     *
     * @queryParam name string 设施名称
     * @queryParam mark string 编号
     * @queryParam address string 设施位置
     * @queryParam location point 经纬度-点 115.24556 25.14551
     * @queryParam type string 设施类型
     * @queryParam status int 设施状态
     * @queryParam rfid int RFID绑定
     * @queryParam remake string 备注
     * @queryParam files->file->local_url string 附件url地址
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $greenDevice = new GreenDevice;
        $data = $greenDevice->getList($request);
        return response()->json(encodeMsg($data,200));
    }

    /**
     * 新增基础设施
     *
     * @bodyParam name string 设施名称
     * @bodyParam mark string 编号
     * @bodyParam address string 设施位置
     * @bodyParam location point 经纬度-点 115.24556 25.14551
     * @bodyParam type string 设施类型
     * @bodyParam status int 设施状态
     * @bodyParam rfid int RFID绑定
     * @bodyParam remake string 备注
     * @bodyParam green_device_ids string 附件ids:1,2,3
     *
     * @queryParam name string 设施名称
     * @queryParam mark string 编号
     * @queryParam address string 设施位置
     * @queryParam location point 经纬度-点 115.24556 25.14551
     * @queryParam type string 设施类型
     * @queryParam status int 设施状态
     * @queryParam rfid int RFID绑定
     * @queryParam remake string 备注
     * @queryParam green_device_ids string 附件ids:1,2,3
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        DB::beginTransaction();
        try {
            $data = $request->all();
            $location = $request->input('location','');
            if(!empty($location)){
                $data['location'] = Point::fromString($location);
            }
            $greenDevice = GreenDevice::create($data);
            if($request->has('green_device_ids')){
                $file_data = [];
                $fileArr = explode(',',$request->get('green_device_ids'));
                foreach ($fileArr as $k=>$value){
                    $tmp = [];
                    $tmp['file_id'] = $value;
                    $tmp['green_device_id'] = $greenDevice->id;
                    array_push($file_data,$tmp);
                }
                try{
                    GreenDeviceFile::insert($file_data);
                }catch (\Exception $e){
                    DB::rollBack();
                    return response()->json(encodeMsg(["message" => "请先确认图片是否已经添加或者图片id是否重复".$e->getMessage()],404), 404);
                }
            }
            DB::commit();
            return response()->json(encodeMsg($greenDevice,201));
        } catch (QueryException $e) {
            DB::rollBack();
            // dd($e);
            return response()->json(encodeMsg(['message' => '基础设施添加失败!'],400));
        }
    }

    /**
     * 查看基础设施
     *
     * @bodyParam id int required 设施ID
     *
     * @queryParam name string 设施名称
     * @queryParam mark string 编号
     * @queryParam address string 设施位置
     * @queryParam location point 经纬度-点 115.24556 25.14551
     * @queryParam type string 设施类型
     * @queryParam status int 设施状态
     * @queryParam rfid int RFID绑定
     * @queryParam remake string 备注
     * @queryParam files->file->local_url string 附件url地址
     */
    public function show($id)
    {
        $data = GreenDevice::with('files')->find($id);
        if($data == []) {
            return response()->json(encodeMsg(["message" => "没有该基础设施信息!"], 404));
        } else {
            return response()->json(encodeMsg($data,200));
        }
    }


    /**
     * 修改基础设施
     *
     * @bodyParam id int required 设施ID
     * @bodyParam name string 设施名称
     * @bodyParam mark string 编号
     * @bodyParam address string 设施位置
     * @bodyParam location point 经纬度-点 115.24556 25.14551
     * @bodyParam type string 设施类型
     * @bodyParam status int 设施状态
     * @bodyParam rfid int RFID绑定
     * @bodyParam remake string 备注
     * @bodyParam green_device_ids string 附件ids:1,2,3
     *
     * @queryParam name string 设施名称
     * @queryParam mark string 编号
     * @queryParam address string 设施位置
     * @queryParam location point 经纬度-点 115.24556 25.14551
     * @queryParam type string 设施类型
     * @queryParam status int 设施状态
     * @queryParam rfid int RFID绑定
     * @queryParam remake string 备注
     * @queryParam green_device_ids string 附件ids:1,2,3
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $greenDevice = GreenDevice::find($id);
        if($greenDevice == []) {
            return response()->json(encodeMsg(["message" => "没有该市政部件信息!"], 404));
        }
        DB::beginTransaction();
        try {
            $data = $request->all();
            $location = $request->input('location','');
            if(!empty($location)){
                $data['location'] = Point::fromString($location);
            }
            if($request->has('green_device_ids')){
                $origin_files = GovenmentDeviceFile::where('green_device_id',$id)->get()->toArray();//图片原来的id号是好多 取出
                $origin_file_ids = array();//这个数组用于报错以前的所有file的id号
                if($origin_files != []){
                    foreach ($origin_files as $v){
                        array_push($origin_file_ids,$v['id']);
                    }
                }
                $file_data = [];
                $fileArr = explode(',',$request->get('green_device_ids'));
                foreach ($fileArr as $k=>$value){
                    $tmp = [];
                    $tmp['file_id'] = $value;
                    $tmp['green_device_id'] = $id;
                    array_push($file_data,$tmp);
                }
                try{
                    GreenDeviceFile::whereIn('id',$origin_file_ids)->delete();
                    GreenDeviceFile::insert($file_data);
                }catch (\Exception $e){
                    DB::rollBack();
                    return response()->json(encodeMsg(["message" => "请先确认图片是否已经添加或者图片id是否重复".$e->getMessage()],404), 404);
                }
            }
            $greenDevice->update($data);
            DB::commit();
            return response()->json(encodeMsg($greenDevice,200));
        } catch (QueryException $e) {
            DB::rollBack();
            return response()->json(encodeMsg(['message' => '基础设施修改失败!'],400));
        }
    }

    /**
     * 删除基础设施
     *
     *
     * @bodyParam id int required 基础设施ID
     * @response 204 {
     *   "message": "基础设施删除成功"
     * }
     *
     *
     * @response 404 {
     *  "message": "没有该基础设施信息"
     * }
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $greenDevice = GreenDevice::find($id);
        if($greenDevice == []) {
            return response()->json(encodeMsg(["message" => "没有该基础设施信息!"], 404));
        }
        if($greenDevice->delete()) {
            return response()->json(encodeMsg(["message" => "基础设施删除成功!"], 204));
        } else {
            return response()->json(encodeMsg(["message" => "基础设施删除失败"], 400));
        }
    }

    /**
     * 批量删除基础设施
     *
     * @bodyParam ids int required id集合字符串[格式：ids=1,2,3]
     * @response 204 {
     *   "message": "基础设施删除成功"
     * }
     *
     *
     */
    public function delete($ids)
    {
        $listId = explode(',', $ids);
        GreenDevice::whereIn('id',$listId)->delete();
        return response()->json(encodeMsg(["message" => "基础设施删除成功!"], 204));
    }
}
